package com.gxa.saas.portal.controller.btopfore;


import com.gxa.saas.core.utils.JwtUtil;
import com.gxa.saas.portal.entity.vo.wylvo.CreateShopVo;
import com.gxa.saas.portal.entity.vo.wylvo.GoodsVo;
import com.gxa.saas.portal.entity.vo.wylvo.ShareVo;
import com.gxa.saas.portal.entity.vo.wylvo.StoreDataVo;
import com.gxa.saas.portal.service.WGoodsService;
import com.gxa.saas.portal.service.WStoreService;
import com.gxa.saas.portal.util.ImageUpload;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wstore")
public class StoreManagement {

    @Resource
    private WStoreService wStoreService;

    @Resource
    private WGoodsService wGoodsService;



    @ApiOperation("店铺管理显示数据获取")
    @GetMapping("/storeData")
    @Transactional
    public Map storeData(HttpServletRequest request){

        //根据登录账号获取商铺id
        String sid= JwtUtil.getUsername(request.getAttribute("token1").toString());
        Integer shopId = wGoodsService.selectShopId(sid);
        StoreDataVo storeData = wStoreService.selectPer(shopId);
        List<GoodsVo> list = wStoreService.storeData(shopId);


        storeData.setGoods(list);
        List<StoreDataVo> list1 = new ArrayList<>();
        list1.add(storeData);
        Map map = new HashMap();
        map.put("msg","查询成功");
        map.put("code",200);
        map.put("data",list1);
        return map;

    }

    @ApiOperation("分享数据修改")
    @PostMapping("/updateShare")
    public Map updateShareP( HttpServletRequest request,@ModelAttribute @Validated ShareVo sv){


        MultipartFile picture = sv.getFile();

        String pic=null;
        //图片地址
        try {
            pic = ImageUpload.upload(picture);

        } catch (IOException e) {
            e.printStackTrace();
        }
        sv.setPicture(pic);

        String sid= JwtUtil.getUsername(request.getAttribute("token1").toString());
        Integer shopId = wGoodsService.selectShopId(sid);
        sv.setId(shopId);
        wStoreService.updateShareP(sv);

        System.out.println(sv);
        Map map = new HashMap();
        map.put("msg","修改成功");
        map.put("code",200);

        return map;
    }

    @ApiOperation("修改商品数据")
    @PostMapping("/updateGoods")
    @Transactional
    //暂时使用@RequestBody后面使用@ModelAttribute 传图片
    public Map updateGoods(@ModelAttribute @Validated GoodsVo goods){

        @NotEmpty(message = "产品价格") String prPrice = goods.getPrPrice();
        goods.setPrPric(BigDecimal.valueOf(Long.parseLong(prPrice)));


        wStoreService.updateGoods(goods);

        MultipartFile picture = goods.getPicture();

        String pic=null;
        //图片地址
        try {
            pic = ImageUpload.upload(picture);

        } catch (IOException e) {
            e.printStackTrace();
        }
        goods.setPrPicture(pic);
        wStoreService.updatePic(goods.getId(),goods.getPrPicture());

        System.out.println(goods);
        Map map = new HashMap();
        map.put("msg","修改成功");
        map.put("code",200);
        return map;
    }

    @ApiOperation("删除商品数据")
    @PostMapping("/delectGoods")
    public Map delectGoods(@NotEmpty(message = "商品id")Integer id){

        wStoreService.delectGoods(id);

        System.out.println(id);
        Map map = new HashMap();
        map.put("msg","删除成功");
        map.put("code",200);
        return map;
    }

    @ApiOperation("创建店铺")
    @PostMapping("/createShop")
    public Map createShop(@RequestBody @Validated CreateShopVo createShopVo){

        String s = wStoreService.selectByShopId(createShopVo.getSLoginId());
        if(s==null||s==""){
        wStoreService.createShop(createShopVo);
        wStoreService.insertAccount(createShopVo.getSLoginId());
        Map map = new HashMap();
        map.put("msg","创建成功");
        map.put("code",200);
        return map;}
        Map map = new HashMap();
        map.put("msg","创建失败，账号已存在");
        map.put("code",233);
        return map;
    }

}
